﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace SurehealthForms
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
         
        }
        string AccountType;

        /// <summary>
        /// the method deployed upon clicking the Login button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (Authenticate(textBox1.Text + ".Xml", textBox1.Text) == true)
            {
                switch (AccountType)
                {
                    case "Cashier":
                        this.Hide();
                        CashierLogin.CashierInit CashierFormInstance = new CashierLogin.CashierInit();
                        CashierFormInstance.ShowDialog();
                        CashierFormInstance.Dispose();
                        this.Show();
                        
                        textBox1.Text = "";
                        break;

                    case "Pharmacist":
                        this.Hide();
                        PharmacistInit pharmacistInstance = new PharmacistInit();
                        pharmacistInstance.ShowDialog();
                        this.Show();
                        textBox1.Text = "";
                        break;

                    case "Manager":
                        this.Hide();
                        Manager.ManagerInit managerinitInstance = new Manager.ManagerInit();
                        managerinitInstance.ShowDialog();
                        this.Show();
                        textBox1.Text = "";



                        break;
                }
            }
            else
            {
            }

            #region Resets the weekly sales
            DateTime s = DateTime.Now;
            DayOfWeek theDay = s.DayOfWeek;

            if (theDay == DayOfWeek.Monday)
            {
                List<string> stringlist = new List<string>();
                StreamReader reader = new StreamReader("TotalSales.txt");
                while (reader.EndOfStream == false)
                {
                    stringlist.Add(reader.ReadLine());
                }
                reader.Close();
                if (stringlist[1] == "false")
                {
                    StreamWriter writer = new StreamWriter("TotalSales.txt");
                    writer.WriteLine("0.00");
                    writer.WriteLine("true");
                    writer.Close();
                }   
            }
            if (theDay == DayOfWeek.Wednesday)
            {
                string temp1, temp2;
                StreamReader reader = new StreamReader("TotalSales.txt");
                temp1 = reader.ReadLine();
                temp2 = "false";
                reader.Close();
                StreamWriter Writer = new StreamWriter("TotalSales.txt");
                Writer.WriteLine(temp1);
                Writer.WriteLine(temp2);
                Writer.Close();
            }
            #endregion

        }

        /// <summary>
        /// XML interaction method for reading in XML and then confirming the password is identical
        /// Returns a true or false;
        /// </summary>
        /// <param name="filename">the filename that will be read</param>
        /// <param name="userNameEntered">textbox1's text and will be used for confirmation</param>
        /// <param name="passwordEntered">textbox2's text that will be used to confirm the password matches the file on record.</param>
        /// <returns></returns>
        private bool Authenticate(string filename,string userNameEntered)
        {           
            string username;
            try
            {
                XmlDocument document = null; ;
                document = new XmlDocument();
                document.Load(filename);
                System.Xml.XmlElement root = document.DocumentElement;
                Console.WriteLine(root.Name);
                username = root.GetAttribute("Name");
                Console.WriteLine(username);
                XmlNode AccountTypeNode = root["AccountType"];
                AccountType = AccountTypeNode.FirstChild.Value;

                return true;
            }
            catch
            {
                textBox1.Text = "";
                MessageBox.Show("account not found, please note account's are case sensitive.");
                return false;
            }
            
        }

    }
}
